////////////////////////////////////////////////////////////////////////////////
//
// Aranya Code (Unit Test)
// author: TonyHack
// E-mail: tonyjobmails@gmail.com
//
// filename: mail.cpp
// function: xml解析类xml_parser单元测试
//
////////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include "xml_parser.h"
#include "type.h"
#include "gtest.h"

using namespace std;

////////////////////////////////////////////////////////
/// 测试固件类
class xml_parser_test : public testing::Test
{
	public:
		xml_parser_test()
		{
			bzero(spider_version, sizeof(spider_version));
			bzero(server_version, sizeof(server_version));
			bzero(username, sizeof(username));
			bzero(password, sizeof(password));
			timeout = 0;
		}
		~xml_parser_test()
		{}

		/// 测试的准备工作
		virtual void SetUp()
		{
		}

		/// 测试完成后清理工作
		virtual void TearDown()
		{
		}

	protected:
		xml_parser xml;			/// xml解析对象
		char spider_version[32];	/// 解析目标配置文件的版本
		char server_version[32];	/// 解析目标配置文件中服务器的版本
		char username[32];		/// 用户名
		char password[32];		/// 密码
		unsigned int timeout;		/// 超时时间
};
////////////////////////////////////////////////////////

int main(int argc, char* argv[])
{
	///////////////////////////////////////////////////////////
	/// 定义xml对象，并加载文件
	xml_parser xml;
	if(!xml.init_file("test.xml"))
	{
		cout << "fail to load test.xml" << endl;
		return 0;
	}
	///////////////////////////////////////////////////////////

	///////////////////////////////////////////////////////////
	/// 定义xml根结点
	const tiny_xml_elem* root = NULL;
	root = xml.get_root_node("spider");
	if(!root)
	{
		cout << "error in get root node: <spider>" << endl;
		return 0;
	}
	///////////////////////////////////////////////////////////
	
	///////////////////////////////////////////////////////////
	/// 中间结点解析
	char spider_version[32];
	bzero(spider_version, sizeof(spider_version));
	if(!xml.get_node_attribute_string(root, spider_version, sizeof(spider_version), "version"))
	{
		cout << "error in get attribute: <spider version>" << endl;
		return 0;
	}
	
	const tiny_xml_elem* server = NULL;
	server = xml.get_child_node(root, "server");
	if(!server)
	{
		cout << "error in get child node: <server>" << endl;
		return 0;
	}
	
	char server_version[32];
	bzero(server_version, sizeof(server_version));
	if(!xml.get_node_attribute_string(server, server_version, sizeof(server_version), "version"))
	{
		cout << "error in get attribute: <server version>" << endl;
		return 0;
	}
	
	const tiny_xml_elem* username = NULL;
	username = xml.get_child_node(server, "username");
	if(!username)
	{
		cout << "error in get child node: <username>" << endl;
		return 0;
	}
	
	char username_value[32];
	bzero(username_value, sizeof(username_value));
	if(!xml.get_node_attribute_string(username, username_value, sizeof(username_value), "value"))
	{
		cout << "error in get attribute: <username value>" << endl;
		return 0;
	}
	
	const tiny_xml_elem* password = NULL;
	password = xml.get_child_node(server, "password");
	if(!password)
	{
		cout << "error in get child node: <password>" << endl;
		return 0;
	}

	char password_value[32];
	bzero(password_value, sizeof(password_value));
	if(!xml.get_node_attribute_string(password, password_value, sizeof(password_value), "value"))
	{
		cout << "error in get attribute: <password value>" << endl;
		return 0;
	}
	
	const tiny_xml_elem* server_set = NULL;
	server_set = xml.get_child_node(server, "set");
	if(!server_set)
	{
		cout << "error in get child node: <set>" << endl;
		return 0;
	}
	
	unsigned int timeout_value = 0;
	const tiny_xml_elem* timeout = NULL;
	timeout = xml.get_child_node(server_set, "timeout");
	if(!timeout || !xml.get_node_attribute_num(timeout, &timeout_value, sizeof(timeout_value), "value"))
	{
		cout << "error in get child node: <set timeout>" << endl;
		return 0;
	}
	///////////////////////////////////////////////////////////
#if 0
	///////////////////////////////////////////////////////////
	/// 测试输出
	cout << "print test:" << endl;
	cout << "	spider_version:" << spider_version << endl;
	cout << "	server_version:" << server_version << endl;
	cout << "	user_value:" << username_value << endl;
	cout << "	password:" << password_value << endl;
	cout << "	timeout:" << timeout_value << endl;
	///////////////////////////////////////////////////////////
	

	///////////////////////////////////////////////////////////
	/// google test宏测试
	EXPECT_STRCASEEQ(spider_version, "xmltest 0.0.1");	/// 测试两个字符串是否相等(不区分大小写)
	EXPECT_STREQ(server_version, "server 0.0.1");		/// 测试两个字符串是否相等(区分大小写)
	EXPECT_STREQ(username_value, "aranya");			/// 测试两个字符串是否相等(区分大小写)
	EXPECT_STREQ(password_value, "best");			/// 测试两个字符串是否相等(区分大小写)
	EXPECT_EQ(timeout_value, (unsigned int)40);		/// 测试两个值是否相等
	///////////////////////////////////////////////////////////
#endif
	///////////////////////////////////////////////////////////
	/// google test 执行用例
	testing::InitGoogleTest(&argc, argv);	/// 初始化命令行
	cout << "running case, and return " << RUN_ALL_TESTS();		/// 运行所有的测试(TEST函数)
	///////////////////////////////////////////////////////////
	return 0;
}

bool is_even(int input)
{
	if(input % 2 == 0)
		return true;
	else
		return false;
}

TEST(even, error_input)
{
	EXPECT_TRUE(is_even(2));
	EXPECT_TRUE(is_even(3));
	EXPECT_TRUE(is_even(4));
}

TEST(even, right_input)
{
	EXPECT_TRUE(is_even(2));
	EXPECT_FALSE(is_even(3));
	EXPECT_TRUE(is_even(4));
}

